<?php
/**
 * gon
 */
namespace app\common\model;

use think\Model;

class CommonModel extends Model
{
	/**
	 * 获取一条数据
	 */
	public function fetch($filter, $field = '*')
	{
		$res = $this->where($filter)->field($field)->find();
		$res = $res ? $res->toArray() : [];
		return $res;
	}

	/**
	 * 获取多条数据
	 */
	public function fetchAll($filter, $field = '*', $order = null)
	{
		if(is_null($order)){
			$res = $this->where($filter)->field($field)->select();
		}else{
			$res = $this->where($filter)->field($field)->order($order)->select();
		}
		$res = $res ? $res->toArray() : [];
		return $res;
	}

	/**
	 * 插入数据
	 */
	public function addData($data)
	{
		$res = $this->insert($data);
		return $res;
	}

	/**
	 * 更新数据
	 */
	public function editData($filter, $data)
	{
		$res = $this->where($filter)->update($data);
		return $res;
	}

	/**
	 * 统计数据
	 */
	public function countData($filter, $field)
	{
		$res = $this->where($filter)->count($field);
		return $res;
	}

	/**
	 * 统计总和数据
	 */
	public function sumData($filter, $field)
	{
		$res = $this->where($filter)->sum($field);
		return $res;
	}
}